package com.cyq.dascma.handler.valida.impl;

import com.cyq.dascma.constant.ValidaEnum;
import com.cyq.dascma.entity.Rule;
import com.cyq.dascma.exception.ServiceException;
import com.cyq.dascma.handler.valida.Valida;
import com.cyq.dascma.result.ResultGenerator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 *
 * dingwen
 * 2021/4/4 22:15
 **/
public class RuleValidaImpl implements Valida<Rule> {
    @Override
    public void valida(ValidaEnum validaEnum, Rule rule) {
        if (ObjectUtils.isEmpty(rule)) {
            throw new ServiceException(ResultGenerator.genFailureResult("rule不能为空").setCode(400));
        }
        switch (validaEnum) {
            case ADD:
                if (!StringUtils.hasText(rule.getCourseId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程编号不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getTeacherId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("授课教师编号不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getRuleAttendance().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("考勤成绩占比不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getRuleClazz().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("平时上课成绩占比不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getRuleHomework().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("作业成绩占比不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getRuleMiddle().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("期中成绩占比不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getRuleEnd().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("期末成绩占比不能为空").setCode(400));
                }
                break;
            case MODIFY:
                if (!StringUtils.hasText(rule.getCourseId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程编号不能为空").setCode(400));
                }
                if (!StringUtils.hasText(rule.getTeacherId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("授课教师编号不能为空").setCode(400));
                }
        }
    }
}
